package com.jiyi.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.jiyi.inteface.PickPopInterface;
import com.swift.maintenance.R;

public class PickPopView {

	private PickPopInterface mPickPopInterface;
	private Context context;
	private PopupWindow mPopupWindow;

	public PickPopView(Context context, PickPopInterface pickPopInterface) {
		this.context = context;
		this.mPickPopInterface = pickPopInterface;
		init();
	}

	/**
	 * @author: HYM
	 * @Title: init
	 * @Description: TODO(初始化一个新的PopWindow)
	 * @param:
	 * @return: void
	 * @return: 2017-5-5
	 * @throws
	 */
	private void init() {
		// 得到内容布局
		View view = LayoutInflater.from(context).inflate(R.layout.pop_pick,
				null);

		// 取消按钮
		TextView tvCancle = (TextView) view.findViewById(R.id.tvNo);
		tvCancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mPickPopInterface.OnCancleListener();
				hidePopWindow();
			}
		});

		// 拍摄按钮
		TextView tvShoot = (TextView) view.findViewById(R.id.tvShoot);
		tvShoot.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mPickPopInterface.OnShootListener();
			}
		});
		// 从相册选择按钮
		TextView tvAlbum = (TextView) view.findViewById(R.id.tvAlbum);
		tvAlbum.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mPickPopInterface.OnAlbumListener();
			}
		});

		mPopupWindow = new PopupWindow(view,
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		mPopupWindow.setFocusable(true); // 让popWindow获取焦点
		mPopupWindow.setOutsideTouchable(true);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable()); // 设置popWindow背景颜色
		mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
		mPopupWindow.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				hidePopWindow();
			}
		});
	}

	/**
	 * @author: HYM
	 * @Title: showPopWindow
	 * @Description: TODO(显示弹出菜单)
	 * @param:
	 * @return: void
	 * @return: 2017-4-26
	 * @throws
	 */
	public void showPopWindow() {
		mPopupWindow
				.showAtLocation(new TextView(context), Gravity.BOTTOM, 0, 0);
		backgroundAlpha(0.5F);
		mPickPopInterface.OnShowListener();
	}

	/**
	 * @author: HYM
	 * @Title: hidePopWindow
	 * @Description: TODO(关闭弹出菜单)
	 * @param:
	 * @return: void
	 * @return: 2017-4-26
	 * @throws
	 */
	public void hidePopWindow() {
		if (mPopupWindow.isShowing()) {
			mPopupWindow.dismiss();
		}
		backgroundAlpha(1F);
		mPickPopInterface.OnHideListener();
	}

	/**
	 * 设置添加屏幕的背景透明度
	 * 
	 * @param bgAlpha
	 */
	public void backgroundAlpha(float bgAlpha) {
		Activity activity = (Activity) context;
		WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
		lp.alpha = bgAlpha; // 0.0-1.0
		activity.getWindow().setAttributes(lp);

		if (bgAlpha == 1)
		{
			activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		}
		else
		{
			activity.getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		}

	}

}
